package com.xgj.ph.dao.impl;

import com.xgj.ph.dao.GoodsDao;
import com.xgj.ph.entity.Goods;
import com.xgj.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class goodsDaoImpl implements GoodsDao {
  @Override
  public int insert(Goods goods) {
    String sql = "insert into goods values(null,?,?,?,?,?,?,?,?,?,?,?)";
    Object obj[] = {
            goods.getGoodsName(),
            goods.getGoodsPresentPrice(),
            goods.getGoodsSalesVolume(),
            goods.getGoodsImage(),
            goods.getAddTime(),
            goods.getGoodsStatus(),
            goods.getShopId(),
            goods.getGoodsShopName(),
            goods.getGoodsCartCount(),
            goods.getShopCategoryId(),
            goods.getGoodsUnit()
    };
    return JDBCDruidUtil.update(sql,obj);
  }
  
  @Override
  public int update(Goods goods) {
    String sql = "update goods set" +
//            "goodsName = ?," +
//            "goodsPresentPrice = ?," +
//            "goodsSalesVolume = ?," +
//            "goodsImage = ?," +
            " goodsStatus = ?" +
//            "shopId = ?," +
//            "goodsShopName = ?," +
//            "goodsCartCount = ?," +
//            "shopCategoryId = ?," +
//            "goodsUnit = ?" +
            " where goodsId = ?";
    Object obj[] = {
//            goods.getGoodsName(),
//            goods.getGoodsPresentPrice(),
//            goods.getGoodsSalesVolume(),
//            goods.getGoodsImage(),
            goods.getGoodsStatus(),
//            goods.getShopId(),
//            goods.getGoodsShopName(),
//            goods.getGoodsCartCount(),
//            goods.getShopCategoryId(),
//            goods.getGoodsUnit(),
            goods.getGoodsId()
    };
    return JDBCDruidUtil.update(sql,obj);
  }
  
  @Override
  public int delete(int goodsId) {
    String sql = "delete from goods where goodsId = ?";
    Object obj[] = {goodsId};
    return JDBCDruidUtil.update(sql,obj);
  }


  @Override
  public List<Goods> selectAll(Map<String, String> map) {
    List<Goods> list = new ArrayList<>();
    String sql = "select * from goods where 1=1";
    List<Object> params = new ArrayList<>();
    String goodsName = map.get("goodsName");
    if (goodsName != null && !"".equals(goodsName)){
      sql += " and goodsName like ?";
      params.add("%"+goodsName+"%");
    }
    String goodsShopName = map.get("goodsShopName");
    if (goodsShopName != null && !"".equals(goodsShopName)){
      sql += " and goodsShopName like ?";
      params.add("%"+goodsShopName+"%");
    }
    String startTime = map.get("startTime");
    if (startTime != null && !"".equals(startTime)){
      sql += " and addTime between ? and ?";
      params.add(startTime);
      params.add(map.get("endTime"));
    }
    String goodsStatus = map.get("goodsStatus");
    System.out.println(goodsStatus);
    if (goodsStatus!=null && !"".equals(goodsStatus)){
      sql+=" and goodsStatus = ? ";
      params.add(Integer.parseInt(goodsStatus));
    }
    String num = map.get("pageNum");
    if (num != null && !"".equals(num)){
      sql += " limit ?,?";
      int pageSize = Integer.parseInt(map.get("pageSize"));
      int pageNum = Integer.parseInt(num);
      params.add((pageNum-1)*pageSize);
      params.add(pageSize);
    }

    Object[] objects = params.toArray();
    ResultSet query = JDBCDruidUtil.query(sql, objects);
    while (true) {
      try {
        if (!query.next()) break;
        list.add(new Goods(
                query.getInt(1),
                query.getString(2),
                query.getInt(3),
                query.getInt(4),
                query.getString(5),
                query.getDate(6),
                query.getInt(7),
                query.getInt(8),
                query.getString(9),
                query.getString(10),
                query.getInt(11),
                query.getString(12)
        ));
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
    JDBCDruidUtil.close();
    return list;
  }
  
  @Override
  public int selectCount(Map<String, String> map) {
    String sql = "select count(1) from goods where 1=1";
    ResultSet query = JDBCDruidUtil.query(sql, null);
    int count = 0;
    try {
      if (query.next()){
        count = query.getInt(1);
      }
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    JDBCDruidUtil.close();
    return count;
  }
}
